/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.library;

import javax.swing.Action;

import nemos.swing.IConverter;
import nemos.swing.swixml.adapter.ConverterAdapter;

import org.apache.felix.scr.annotations.Component;
import org.osgi.framework.ServiceReference;
import org.swixml.Converter;
import org.swixml.ConverterLibrary;

/**
 * ConverterLibraryManager : The SwiXML converter library manager.
 * 
 * <p>
 * This component manages the SwiXML {@link ConverterLibrary}, providing it
 * adapted OSGi {@link IConverter} services.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Component
public class ConverterLibraryManager extends
        AbstractLibraryManager<IConverter, Converter<?>, Class<?>> {

    /** The managed library */
    private final ConverterLibrary mLibrary;

    /**
     * Constructor
     */
    public ConverterLibraryManager() {
        super();

        mLibrary = ConverterLibrary.getInstance();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#getCurrentService(java.lang.Object)
     */
    @Override
    protected Converter<?> getCurrentService(final Class<?> pType) {
        return mLibrary.getConverter(pType);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#wrap(java.lang.Object,
     *      java.lang.Object)
     */
    @Override
    protected Converter<?> wrap(@SuppressWarnings("unused") final Class<?> pType,
            final IConverter pConverter) {
        return new ConverterAdapter(pConverter);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#register(java.lang.Object,
     *      java.lang.Object)
     */
    @Override
    protected void register(final Class<?> pType, final Converter<?> pConverter) {
        if (!Action.class.equals(pType)) {
            mLibrary.register(pType, pConverter);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#getConverterKey(org.osgi.framework.ServiceReference,
     *      java.lang.Object)
     */
    @Override
    protected Class<?> getConverterKey(
            @SuppressWarnings("unused") final ServiceReference<IConverter> pReference,
            final IConverter pConverter) {
        return pConverter.getType();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#getConverterType()
     */
    @Override
    protected String getConverterType() {
        return IConverter.class.getName();
    }
}
